{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The JOIN operation\n",
    "\n",
    "![rel](https://sqlzoo.net/w/images/c/ce/FootballERD.png)\n",
    "\n",
    "## JOIN and UEFA EURO 2012\n",
    "\n",
    "This tutorial introduces `JOIN` which allows you to use data from two or more tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.\n",
    "\n",
    "The data is available (mysql format) at <http://sqlzoo.net/euro2012.sql>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "-- \u001b[1mAttaching packages\u001b[22m --------------------------------------- tidyverse 1.3.0 --\n",
      "\n",
      "\u001b[32mv\u001b[39m \u001b[34mggplot2\u001b[39m 3.3.0     \u001b[32mv\u001b[39m \u001b[34mpurrr  \u001b[39m 0.3.4\n",
      "\u001b[32mv\u001b[39m \u001b[34mtibble \u001b[39m 3.0.1     \u001b[32mv\u001b[39m \u001b[34mdplyr  \u001b[39m 0.8.5\n",
      "\u001b[32mv\u001b[39m \u001b[34mtidyr  \u001b[39m 1.0.2     \u001b[32mv\u001b[39m \u001b[34mstringr\u001b[39m 1.4.0\n",
      "\u001b[32mv\u001b[39m \u001b[34mreadr  \u001b[39m 1.3.1     \u001b[32mv\u001b[39m \u001b[34mforcats\u001b[39m 0.5.0\n",
      "\n",
      "-- \u001b[1mConflicts\u001b[22m ------------------------------------------ tidyverse_conflicts() --\n",
      "\u001b[31mx\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n",
      "\u001b[31mx\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m    masks \u001b[34mstats\u001b[39m::lag()\n",
      "\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Password? ·········\n"
     ]
    }
   ],
   "source": [
    "library(tidyverse)\n",
    "library(DBI)\n",
    "library(getPass)\n",
    "drv <- switch(Sys.info()['sysname'],\n",
    "             Windows=\"PostgreSQL Unicode(x64)\",\n",
    "             Darwin=\"/usr/local/lib/psqlodbcw.so\",\n",
    "             Linux=\"PostgreSQL\")\n",
    "con <- dbConnect(\n",
    "  odbc::odbc(),\n",
    "  driver = drv,\n",
    "  Server = \"localhost\",\n",
    "  Database = \"sqlzoo\",\n",
    "  UID = \"postgres\",\n",
    "  PWD = getPass(\"Password?\"),\n",
    "  Port = 5432\n",
    ")\n",
    "options(repr.matrix.max.rows=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "The first example shows the goal scored by a player with the last name 'Bender'. The `*` says to list all the columns in the table - a shorter way of saying `matchid, teamid, player, gtime`\n",
    "\n",
    "**Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: `teamid = 'GER'`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "game <- dbReadTable(con, 'game')\n",
    "goal <- dbReadTable(con, 'goal')\n",
    "eteam <- dbReadTable(con, 'eteam')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 10 × 2</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>matchid</th><th scope=col>player</th></tr>\n",
       "\t<tr><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>1008</td><td>Mario Gómez   </td></tr>\n",
       "\t<tr><td>1010</td><td>Mario Gómez   </td></tr>\n",
       "\t<tr><td>1010</td><td>Mario Gómez   </td></tr>\n",
       "\t<tr><td>1012</td><td>Lukas Podolski</td></tr>\n",
       "\t<tr><td>1012</td><td>Lars Bender   </td></tr>\n",
       "\t<tr><td>1026</td><td>Philipp Lahm  </td></tr>\n",
       "\t<tr><td>1026</td><td>Sami Khedira  </td></tr>\n",
       "\t<tr><td>1026</td><td>Miroslav Klose</td></tr>\n",
       "\t<tr><td>1026</td><td>Marco Reus    </td></tr>\n",
       "\t<tr><td>1030</td><td>Mesut Özil    </td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 10 × 2\n",
       "\\begin{tabular}{ll}\n",
       " matchid & player\\\\\n",
       " <int> & <chr>\\\\\n",
       "\\hline\n",
       "\t 1008 & Mario Gómez   \\\\\n",
       "\t 1010 & Mario Gómez   \\\\\n",
       "\t 1010 & Mario Gómez   \\\\\n",
       "\t 1012 & Lukas Podolski\\\\\n",
       "\t 1012 & Lars Bender   \\\\\n",
       "\t 1026 & Philipp Lahm  \\\\\n",
       "\t 1026 & Sami Khedira  \\\\\n",
       "\t 1026 & Miroslav Klose\\\\\n",
       "\t 1026 & Marco Reus    \\\\\n",
       "\t 1030 & Mesut Özil    \\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 10 × 2\n",
       "\n",
       "| matchid &lt;int&gt; | player &lt;chr&gt; |\n",
       "|---|---|\n",
       "| 1008 | Mario Gómez    |\n",
       "| 1010 | Mario Gómez    |\n",
       "| 1010 | Mario Gómez    |\n",
       "| 1012 | Lukas Podolski |\n",
       "| 1012 | Lars Bender    |\n",
       "| 1026 | Philipp Lahm   |\n",
       "| 1026 | Sami Khedira   |\n",
       "| 1026 | Miroslav Klose |\n",
       "| 1026 | Marco Reus     |\n",
       "| 1030 | Mesut Özil     |\n",
       "\n"
      ],
      "text/plain": [
       "   matchid player        \n",
       "1  1008    Mario Gómez   \n",
       "2  1010    Mario Gómez   \n",
       "3  1010    Mario Gómez   \n",
       "4  1012    Lukas Podolski\n",
       "5  1012    Lars Bender   \n",
       "6  1026    Philipp Lahm  \n",
       "7  1026    Sami Khedira  \n",
       "8  1026    Miroslav Klose\n",
       "9  1026    Marco Reus    \n",
       "10 1030    Mesut Özil    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "goal %>% \n",
    "    filter(teamid=='GER') %>% \n",
    "    select(matchid, player)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "\n",
    "From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.\n",
    "\n",
    "Notice in the that the column `matchid `in the `goal` table corresponds to the `id` column in the `game` table. We can look up information about game 1012 by finding that row in the `game` table.\n",
    "\n",
    "**Show id, stadium, team1, team2 for just game 1012**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 1 × 4</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>id</th><th scope=col>stadium</th><th scope=col>team1</th><th scope=col>team2</th></tr>\n",
       "\t<tr><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>1012</td><td>Arena Lviv</td><td>DEN</td><td>GER</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 1 × 4\n",
       "\\begin{tabular}{llll}\n",
       " id & stadium & team1 & team2\\\\\n",
       " <int> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t 1012 & Arena Lviv & DEN & GER\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 1 × 4\n",
       "\n",
       "| id &lt;int&gt; | stadium &lt;chr&gt; | team1 &lt;chr&gt; | team2 &lt;chr&gt; |\n",
       "|---|---|---|---|\n",
       "| 1012 | Arena Lviv | DEN | GER |\n",
       "\n"
      ],
      "text/plain": [
       "  id   stadium    team1 team2\n",
       "1 1012 Arena Lviv DEN   GER  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>% \n",
    "    filter(id==1012) %>%\n",
    "    select(id, stadium, team1, team2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "You can combine the two steps into a single query with a JOIN.\n",
    "\n",
    "```sql\n",
    "SELECT *\n",
    "  FROM game JOIN goal ON (id=matchid)\n",
    "```\n",
    "\n",
    "The **FROM** clause says to merge data from the goal table with that from the game table. The **ON** says how to figure out which rows in **game** go with which rows in **goal** - the **matchid** from **goal** must match **id** from **game**. (If we wanted to be more clear/specific we could say\n",
    "`ON (game.id=goal.matchid)`\n",
    "\n",
    "The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.\n",
    "\n",
    "**Modify it to show the player, teamid, stadium and mdate for every German goal.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 10 × 4</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>player</th><th scope=col>teamid</th><th scope=col>stadium</th><th scope=col>mdate</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>Mario Gómez   </td><td>GER</td><td>Arena Lviv              </td><td>9 June 2012 </td></tr>\n",
       "\t<tr><td>Mario Gómez   </td><td>GER</td><td>Metalist Stadium        </td><td>13 June 2012</td></tr>\n",
       "\t<tr><td>Mario Gómez   </td><td>GER</td><td>Metalist Stadium        </td><td>13 June 2012</td></tr>\n",
       "\t<tr><td>Lukas Podolski</td><td>GER</td><td>Arena Lviv              </td><td>17 June 2012</td></tr>\n",
       "\t<tr><td>Lars Bender   </td><td>GER</td><td>Arena Lviv              </td><td>17 June 2012</td></tr>\n",
       "\t<tr><td>Philipp Lahm  </td><td>GER</td><td>PGE Arena Gdansk        </td><td>22 June 2012</td></tr>\n",
       "\t<tr><td>Sami Khedira  </td><td>GER</td><td>PGE Arena Gdansk        </td><td>22 June 2012</td></tr>\n",
       "\t<tr><td>Miroslav Klose</td><td>GER</td><td>PGE Arena Gdansk        </td><td>22 June 2012</td></tr>\n",
       "\t<tr><td>Marco Reus    </td><td>GER</td><td>PGE Arena Gdansk        </td><td>22 June 2012</td></tr>\n",
       "\t<tr><td>Mesut Özil    </td><td>GER</td><td>National Stadium, Warsaw</td><td>28 June 2012</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 10 × 4\n",
       "\\begin{tabular}{llll}\n",
       " player & teamid & stadium & mdate\\\\\n",
       " <chr> & <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t Mario Gómez    & GER & Arena Lviv               & 9 June 2012 \\\\\n",
       "\t Mario Gómez    & GER & Metalist Stadium         & 13 June 2012\\\\\n",
       "\t Mario Gómez    & GER & Metalist Stadium         & 13 June 2012\\\\\n",
       "\t Lukas Podolski & GER & Arena Lviv               & 17 June 2012\\\\\n",
       "\t Lars Bender    & GER & Arena Lviv               & 17 June 2012\\\\\n",
       "\t Philipp Lahm   & GER & PGE Arena Gdansk         & 22 June 2012\\\\\n",
       "\t Sami Khedira   & GER & PGE Arena Gdansk         & 22 June 2012\\\\\n",
       "\t Miroslav Klose & GER & PGE Arena Gdansk         & 22 June 2012\\\\\n",
       "\t Marco Reus     & GER & PGE Arena Gdansk         & 22 June 2012\\\\\n",
       "\t Mesut Özil     & GER & National Stadium, Warsaw & 28 June 2012\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 10 × 4\n",
       "\n",
       "| player &lt;chr&gt; | teamid &lt;chr&gt; | stadium &lt;chr&gt; | mdate &lt;chr&gt; |\n",
       "|---|---|---|---|\n",
       "| Mario Gómez    | GER | Arena Lviv               | 9 June 2012  |\n",
       "| Mario Gómez    | GER | Metalist Stadium         | 13 June 2012 |\n",
       "| Mario Gómez    | GER | Metalist Stadium         | 13 June 2012 |\n",
       "| Lukas Podolski | GER | Arena Lviv               | 17 June 2012 |\n",
       "| Lars Bender    | GER | Arena Lviv               | 17 June 2012 |\n",
       "| Philipp Lahm   | GER | PGE Arena Gdansk         | 22 June 2012 |\n",
       "| Sami Khedira   | GER | PGE Arena Gdansk         | 22 June 2012 |\n",
       "| Miroslav Klose | GER | PGE Arena Gdansk         | 22 June 2012 |\n",
       "| Marco Reus     | GER | PGE Arena Gdansk         | 22 June 2012 |\n",
       "| Mesut Özil     | GER | National Stadium, Warsaw | 28 June 2012 |\n",
       "\n"
      ],
      "text/plain": [
       "   player         teamid stadium                  mdate       \n",
       "1  Mario Gómez    GER    Arena Lviv               9 June 2012 \n",
       "2  Mario Gómez    GER    Metalist Stadium         13 June 2012\n",
       "3  Mario Gómez    GER    Metalist Stadium         13 June 2012\n",
       "4  Lukas Podolski GER    Arena Lviv               17 June 2012\n",
       "5  Lars Bender    GER    Arena Lviv               17 June 2012\n",
       "6  Philipp Lahm   GER    PGE Arena Gdansk         22 June 2012\n",
       "7  Sami Khedira   GER    PGE Arena Gdansk         22 June 2012\n",
       "8  Miroslav Klose GER    PGE Arena Gdansk         22 June 2012\n",
       "9  Marco Reus     GER    PGE Arena Gdansk         22 June 2012\n",
       "10 Mesut Özil     GER    National Stadium, Warsaw 28 June 2012"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>% \n",
    "    inner_join(goal, by=c(id=\"matchid\")) %>% \n",
    "    filter(teamid=='GER') %>%\n",
    "    select(player, teamid, stadium, mdate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "Use the same `JOIN` as in the previous question.\n",
    "\n",
    "**Show the team1, team2 and player for every goal scored by a player called Mario `player LIKE 'Mario%'`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 9 × 3</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>team1</th><th scope=col>team2</th><th scope=col>player</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>GER</td><td>POR</td><td>Mario Gómez    </td></tr>\n",
       "\t<tr><td>NED</td><td>GER</td><td>Mario Gómez    </td></tr>\n",
       "\t<tr><td>NED</td><td>GER</td><td>Mario Gómez    </td></tr>\n",
       "\t<tr><td>IRL</td><td>CRO</td><td>Mario Mandžukic</td></tr>\n",
       "\t<tr><td>IRL</td><td>CRO</td><td>Mario Mandžukic</td></tr>\n",
       "\t<tr><td>ITA</td><td>CRO</td><td>Mario Mandžukic</td></tr>\n",
       "\t<tr><td>ITA</td><td>IRL</td><td>Mario Balotelli</td></tr>\n",
       "\t<tr><td>GER</td><td>ITA</td><td>Mario Balotelli</td></tr>\n",
       "\t<tr><td>GER</td><td>ITA</td><td>Mario Balotelli</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 9 × 3\n",
       "\\begin{tabular}{lll}\n",
       " team1 & team2 & player\\\\\n",
       " <chr> & <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t GER & POR & Mario Gómez    \\\\\n",
       "\t NED & GER & Mario Gómez    \\\\\n",
       "\t NED & GER & Mario Gómez    \\\\\n",
       "\t IRL & CRO & Mario Mandžukic\\\\\n",
       "\t IRL & CRO & Mario Mandžukic\\\\\n",
       "\t ITA & CRO & Mario Mandžukic\\\\\n",
       "\t ITA & IRL & Mario Balotelli\\\\\n",
       "\t GER & ITA & Mario Balotelli\\\\\n",
       "\t GER & ITA & Mario Balotelli\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 9 × 3\n",
       "\n",
       "| team1 &lt;chr&gt; | team2 &lt;chr&gt; | player &lt;chr&gt; |\n",
       "|---|---|---|\n",
       "| GER | POR | Mario Gómez     |\n",
       "| NED | GER | Mario Gómez     |\n",
       "| NED | GER | Mario Gómez     |\n",
       "| IRL | CRO | Mario Mandžukic |\n",
       "| IRL | CRO | Mario Mandžukic |\n",
       "| ITA | CRO | Mario Mandžukic |\n",
       "| ITA | IRL | Mario Balotelli |\n",
       "| GER | ITA | Mario Balotelli |\n",
       "| GER | ITA | Mario Balotelli |\n",
       "\n"
      ],
      "text/plain": [
       "  team1 team2 player         \n",
       "1 GER   POR   Mario Gómez    \n",
       "2 NED   GER   Mario Gómez    \n",
       "3 NED   GER   Mario Gómez    \n",
       "4 IRL   CRO   Mario Mandžukic\n",
       "5 IRL   CRO   Mario Mandžukic\n",
       "6 ITA   CRO   Mario Mandžukic\n",
       "7 ITA   IRL   Mario Balotelli\n",
       "8 GER   ITA   Mario Balotelli\n",
       "9 GER   ITA   Mario Balotelli"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>%\n",
    "    inner_join(goal, by=c(id='matchid')) %>% \n",
    "    filter(str_starts(player, 'Mario')) %>%\n",
    "    select(team1, team2, player)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "\n",
    "The table `eteam` gives details of every national team including the coach. You can `JOIN` `goal` to `eteam` using the phrase goal `JOIN eteam on teamid=id`\n",
    "\n",
    "**Show `player, teamid, coach, gtime` for all goals scored in the first 10 minutes `gtime<=10`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 4 × 4</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>player</th><th scope=col>teamid</th><th scope=col>coach</th><th scope=col>gtime</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>Petr Jirácek   </td><td>CZE</td><td>Michal Bílek      </td><td>3</td></tr>\n",
       "\t<tr><td>Václav Pilar   </td><td>CZE</td><td>Michal Bílek      </td><td>6</td></tr>\n",
       "\t<tr><td>Mario Mandžukic</td><td>CRO</td><td>Slaven Bilic      </td><td>3</td></tr>\n",
       "\t<tr><td>Fernando Torres</td><td>ESP</td><td>Vicente del Bosque</td><td>4</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 4 × 4\n",
       "\\begin{tabular}{llll}\n",
       " player & teamid & coach & gtime\\\\\n",
       " <chr> & <chr> & <chr> & <int>\\\\\n",
       "\\hline\n",
       "\t Petr Jirácek    & CZE & Michal Bílek       & 3\\\\\n",
       "\t Václav Pilar    & CZE & Michal Bílek       & 6\\\\\n",
       "\t Mario Mandžukic & CRO & Slaven Bilic       & 3\\\\\n",
       "\t Fernando Torres & ESP & Vicente del Bosque & 4\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 4 × 4\n",
       "\n",
       "| player &lt;chr&gt; | teamid &lt;chr&gt; | coach &lt;chr&gt; | gtime &lt;int&gt; |\n",
       "|---|---|---|---|\n",
       "| Petr Jirácek    | CZE | Michal Bílek       | 3 |\n",
       "| Václav Pilar    | CZE | Michal Bílek       | 6 |\n",
       "| Mario Mandžukic | CRO | Slaven Bilic       | 3 |\n",
       "| Fernando Torres | ESP | Vicente del Bosque | 4 |\n",
       "\n"
      ],
      "text/plain": [
       "  player          teamid coach              gtime\n",
       "1 Petr Jirácek    CZE    Michal Bílek       3    \n",
       "2 Václav Pilar    CZE    Michal Bílek       6    \n",
       "3 Mario Mandžukic CRO    Slaven Bilic       3    \n",
       "4 Fernando Torres ESP    Vicente del Bosque 4    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "goal %>% \n",
    "    inner_join(eteam, by=c(teamid=\"id\")) %>%\n",
    "    filter(gtime<=10) %>%\n",
    "    select(player, teamid, coach, gtime)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.\n",
    "\n",
    "To `JOIN` `game` with `eteam` you could use either\n",
    "`game JOIN eteam ON (team1=eteam.id)` or `game JOIN eteam ON (team2=eteam.id)`\n",
    "\n",
    "Notice that because `id` is a column name in both `game` and `eteam` you must specify `eteam.id` instead of just `id`\n",
    "\n",
    "**List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 2 × 2</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>mdate</th><th scope=col>teamname</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>12 June 2012</td><td>Greece</td></tr>\n",
       "\t<tr><td>16 June 2012</td><td>Greece</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 2 × 2\n",
       "\\begin{tabular}{ll}\n",
       " mdate & teamname\\\\\n",
       " <chr> & <chr>\\\\\n",
       "\\hline\n",
       "\t 12 June 2012 & Greece\\\\\n",
       "\t 16 June 2012 & Greece\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 2 × 2\n",
       "\n",
       "| mdate &lt;chr&gt; | teamname &lt;chr&gt; |\n",
       "|---|---|\n",
       "| 12 June 2012 | Greece |\n",
       "| 16 June 2012 | Greece |\n",
       "\n"
      ],
      "text/plain": [
       "  mdate        teamname\n",
       "1 12 June 2012 Greece  \n",
       "2 16 June 2012 Greece  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>% \n",
    "    inner_join(eteam, by=c(team1=\"id\")) %>%\n",
    "    filter(coach=='Fernando Santos') %>% \n",
    "    select(mdate, teamname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.\n",
    "\n",
    "**List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 9 × 1</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>player</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>Robert Lewandowski  </td></tr>\n",
       "\t<tr><td>Dimitris Salpingidis</td></tr>\n",
       "\t<tr><td>Alan Dzagoev        </td></tr>\n",
       "\t<tr><td>Jakub Blaszczykowski</td></tr>\n",
       "\t<tr><td>Giorgos Karagounis  </td></tr>\n",
       "\t<tr><td>Cristiano Ronaldo   </td></tr>\n",
       "\t<tr><td>Mario Balotelli     </td></tr>\n",
       "\t<tr><td>Mario Balotelli     </td></tr>\n",
       "\t<tr><td>Mesut Özil          </td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 9 × 1\n",
       "\\begin{tabular}{l}\n",
       " player\\\\\n",
       " <chr>\\\\\n",
       "\\hline\n",
       "\t Robert Lewandowski  \\\\\n",
       "\t Dimitris Salpingidis\\\\\n",
       "\t Alan Dzagoev        \\\\\n",
       "\t Jakub Blaszczykowski\\\\\n",
       "\t Giorgos Karagounis  \\\\\n",
       "\t Cristiano Ronaldo   \\\\\n",
       "\t Mario Balotelli     \\\\\n",
       "\t Mario Balotelli     \\\\\n",
       "\t Mesut Özil          \\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 9 × 1\n",
       "\n",
       "| player &lt;chr&gt; |\n",
       "|---|\n",
       "| Robert Lewandowski   |\n",
       "| Dimitris Salpingidis |\n",
       "| Alan Dzagoev         |\n",
       "| Jakub Blaszczykowski |\n",
       "| Giorgos Karagounis   |\n",
       "| Cristiano Ronaldo    |\n",
       "| Mario Balotelli      |\n",
       "| Mario Balotelli      |\n",
       "| Mesut Özil           |\n",
       "\n"
      ],
      "text/plain": [
       "  player              \n",
       "1 Robert Lewandowski  \n",
       "2 Dimitris Salpingidis\n",
       "3 Alan Dzagoev        \n",
       "4 Jakub Blaszczykowski\n",
       "5 Giorgos Karagounis  \n",
       "6 Cristiano Ronaldo   \n",
       "7 Mario Balotelli     \n",
       "8 Mario Balotelli     \n",
       "9 Mesut Özil          "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "goal %>% \n",
    "    inner_join(game, by=c(matchid=\"id\")) %>% \n",
    "    filter(stadium=='National Stadium, Warsaw') %>% \n",
    "    select(player)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. More difficult questions\n",
    "\n",
    "The example query shows all goals scored in the Germany-Greece quarterfinal.\n",
    "**Instead show the name of all players who scored a goal against Germany.**\n",
    "\n",
    "> __HINT__   \n",
    "> Select goals scored only by non-German players in matches where GER was the id of either **team1** or **team2**.\n",
    "> You can use `teamid!='GER'` to prevent listing German players.\n",
    "> You can use `DISTINCT` to stop players being listed twice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A data.frame: 5 × 1</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>player</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>Robin van Persie    </td></tr>\n",
       "\t<tr><td>Michael Krohn-Dehli </td></tr>\n",
       "\t<tr><td>Georgios Samaras    </td></tr>\n",
       "\t<tr><td>Dimitris Salpingidis</td></tr>\n",
       "\t<tr><td>Mario Balotelli     </td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 5 × 1\n",
       "\\begin{tabular}{l}\n",
       " player\\\\\n",
       " <chr>\\\\\n",
       "\\hline\n",
       "\t Robin van Persie    \\\\\n",
       "\t Michael Krohn-Dehli \\\\\n",
       "\t Georgios Samaras    \\\\\n",
       "\t Dimitris Salpingidis\\\\\n",
       "\t Mario Balotelli     \\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 5 × 1\n",
       "\n",
       "| player &lt;chr&gt; |\n",
       "|---|\n",
       "| Robin van Persie     |\n",
       "| Michael Krohn-Dehli  |\n",
       "| Georgios Samaras     |\n",
       "| Dimitris Salpingidis |\n",
       "| Mario Balotelli      |\n",
       "\n"
      ],
      "text/plain": [
       "  player              \n",
       "1 Robin van Persie    \n",
       "2 Michael Krohn-Dehli \n",
       "3 Georgios Samaras    \n",
       "4 Dimitris Salpingidis\n",
       "5 Mario Balotelli     "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>% \n",
    "    inner_join(goal, by=c(id=\"matchid\")) %>%\n",
    "    filter((team1=='GER' | team2=='GER') &\n",
    "           teamid != 'GER') %>%\n",
    "    select(player) %>%\n",
    "    distinct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.\n",
    "Show teamname and the total number of goals scored.\n",
    "\n",
    "> __COUNT and GROUP BY__  \n",
    "> You should COUNT(*) in the SELECT line and GROUP BY teamname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A tibble: 16 × 2</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>teamname</th><th scope=col>goals</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>Croatia            </td><td> 4</td></tr>\n",
       "\t<tr><td>Czech Republic     </td><td> 4</td></tr>\n",
       "\t<tr><td>Denmark            </td><td> 4</td></tr>\n",
       "\t<tr><td>England            </td><td> 5</td></tr>\n",
       "\t<tr><td>France             </td><td> 3</td></tr>\n",
       "\t<tr><td>Germany            </td><td>10</td></tr>\n",
       "\t<tr><td>Greece             </td><td> 5</td></tr>\n",
       "\t<tr><td>Italy              </td><td> 6</td></tr>\n",
       "\t<tr><td>Netherlands        </td><td> 2</td></tr>\n",
       "\t<tr><td>Poland             </td><td> 2</td></tr>\n",
       "\t<tr><td>Portugal           </td><td> 6</td></tr>\n",
       "\t<tr><td>Republic of Ireland</td><td> 1</td></tr>\n",
       "\t<tr><td>Russia             </td><td> 5</td></tr>\n",
       "\t<tr><td>Spain              </td><td>12</td></tr>\n",
       "\t<tr><td>Sweden             </td><td> 5</td></tr>\n",
       "\t<tr><td>Ukraine            </td><td> 2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A tibble: 16 × 2\n",
       "\\begin{tabular}{ll}\n",
       " teamname & goals\\\\\n",
       " <chr> & <int>\\\\\n",
       "\\hline\n",
       "\t Croatia             &  4\\\\\n",
       "\t Czech Republic      &  4\\\\\n",
       "\t Denmark             &  4\\\\\n",
       "\t England             &  5\\\\\n",
       "\t France              &  3\\\\\n",
       "\t Germany             & 10\\\\\n",
       "\t Greece              &  5\\\\\n",
       "\t Italy               &  6\\\\\n",
       "\t Netherlands         &  2\\\\\n",
       "\t Poland              &  2\\\\\n",
       "\t Portugal            &  6\\\\\n",
       "\t Republic of Ireland &  1\\\\\n",
       "\t Russia              &  5\\\\\n",
       "\t Spain               & 12\\\\\n",
       "\t Sweden              &  5\\\\\n",
       "\t Ukraine             &  2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A tibble: 16 × 2\n",
       "\n",
       "| teamname &lt;chr&gt; | goals &lt;int&gt; |\n",
       "|---|---|\n",
       "| Croatia             |  4 |\n",
       "| Czech Republic      |  4 |\n",
       "| Denmark             |  4 |\n",
       "| England             |  5 |\n",
       "| France              |  3 |\n",
       "| Germany             | 10 |\n",
       "| Greece              |  5 |\n",
       "| Italy               |  6 |\n",
       "| Netherlands         |  2 |\n",
       "| Poland              |  2 |\n",
       "| Portugal            |  6 |\n",
       "| Republic of Ireland |  1 |\n",
       "| Russia              |  5 |\n",
       "| Spain               | 12 |\n",
       "| Sweden              |  5 |\n",
       "| Ukraine             |  2 |\n",
       "\n"
      ],
      "text/plain": [
       "   teamname            goals\n",
       "1  Croatia              4   \n",
       "2  Czech Republic       4   \n",
       "3  Denmark              4   \n",
       "4  England              5   \n",
       "5  France               3   \n",
       "6  Germany             10   \n",
       "7  Greece               5   \n",
       "8  Italy                6   \n",
       "9  Netherlands          2   \n",
       "10 Poland               2   \n",
       "11 Portugal             6   \n",
       "12 Republic of Ireland  1   \n",
       "13 Russia               5   \n",
       "14 Spain               12   \n",
       "15 Sweden               5   \n",
       "16 Ukraine              2   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eteam %>% \n",
    "    inner_join(goal, by=c(id=\"teamid\")) %>%\n",
    "    group_by(teamname) %>% \n",
    "    summarise(goals=n())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.\n",
    "\n",
    "**Show the stadium and the number of goals scored in each stadium.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A tibble: 8 × 2</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>stadium</th><th scope=col>goals</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>Arena Lviv                         </td><td> 9</td></tr>\n",
       "\t<tr><td>Donbass Arena                      </td><td> 7</td></tr>\n",
       "\t<tr><td>Metalist Stadium                   </td><td> 7</td></tr>\n",
       "\t<tr><td>National Stadium, Warsaw           </td><td> 9</td></tr>\n",
       "\t<tr><td>Olimpiyskiy National Sports Complex</td><td>14</td></tr>\n",
       "\t<tr><td>PGE Arena Gdansk                   </td><td>13</td></tr>\n",
       "\t<tr><td>Stadion Miejski (Poznan)           </td><td> 8</td></tr>\n",
       "\t<tr><td>Stadion Miejski (Wroclaw)          </td><td> 9</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A tibble: 8 × 2\n",
       "\\begin{tabular}{ll}\n",
       " stadium & goals\\\\\n",
       " <chr> & <int>\\\\\n",
       "\\hline\n",
       "\t Arena Lviv                          &  9\\\\\n",
       "\t Donbass Arena                       &  7\\\\\n",
       "\t Metalist Stadium                    &  7\\\\\n",
       "\t National Stadium, Warsaw            &  9\\\\\n",
       "\t Olimpiyskiy National Sports Complex & 14\\\\\n",
       "\t PGE Arena Gdansk                    & 13\\\\\n",
       "\t Stadion Miejski (Poznan)            &  8\\\\\n",
       "\t Stadion Miejski (Wroclaw)           &  9\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A tibble: 8 × 2\n",
       "\n",
       "| stadium &lt;chr&gt; | goals &lt;int&gt; |\n",
       "|---|---|\n",
       "| Arena Lviv                          |  9 |\n",
       "| Donbass Arena                       |  7 |\n",
       "| Metalist Stadium                    |  7 |\n",
       "| National Stadium, Warsaw            |  9 |\n",
       "| Olimpiyskiy National Sports Complex | 14 |\n",
       "| PGE Arena Gdansk                    | 13 |\n",
       "| Stadion Miejski (Poznan)            |  8 |\n",
       "| Stadion Miejski (Wroclaw)           |  9 |\n",
       "\n"
      ],
      "text/plain": [
       "  stadium                             goals\n",
       "1 Arena Lviv                           9   \n",
       "2 Donbass Arena                        7   \n",
       "3 Metalist Stadium                     7   \n",
       "4 National Stadium, Warsaw             9   \n",
       "5 Olimpiyskiy National Sports Complex 14   \n",
       "6 PGE Arena Gdansk                    13   \n",
       "7 Stadion Miejski (Poznan)             8   \n",
       "8 Stadion Miejski (Wroclaw)            9   "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>%\n",
    "    inner_join(goal, by=c(id=\"matchid\")) %>%\n",
    "    group_by(stadium) %>% \n",
    "    summarise(goals=n())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.\n",
    "**For every match involving 'POL', show the matchid, date and the number of goals scored.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A grouped_df: 3 × 3</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>id</th><th scope=col>mdate</th><th scope=col>goals</th></tr>\n",
       "\t<tr><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>1001</td><td>8 June 2012 </td><td>2</td></tr>\n",
       "\t<tr><td>1004</td><td>12 June 2012</td><td>2</td></tr>\n",
       "\t<tr><td>1005</td><td>16 June 2012</td><td>1</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A grouped\\_df: 3 × 3\n",
       "\\begin{tabular}{lll}\n",
       " id & mdate & goals\\\\\n",
       " <int> & <chr> & <int>\\\\\n",
       "\\hline\n",
       "\t 1001 & 8 June 2012  & 2\\\\\n",
       "\t 1004 & 12 June 2012 & 2\\\\\n",
       "\t 1005 & 16 June 2012 & 1\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A grouped_df: 3 × 3\n",
       "\n",
       "| id &lt;int&gt; | mdate &lt;chr&gt; | goals &lt;int&gt; |\n",
       "|---|---|---|\n",
       "| 1001 | 8 June 2012  | 2 |\n",
       "| 1004 | 12 June 2012 | 2 |\n",
       "| 1005 | 16 June 2012 | 1 |\n",
       "\n"
      ],
      "text/plain": [
       "  id   mdate        goals\n",
       "1 1001 8 June 2012  2    \n",
       "2 1004 12 June 2012 2    \n",
       "3 1005 16 June 2012 1    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>% \n",
    "    inner_join(goal, by=c(id=\"matchid\")) %>%\n",
    "    filter(team1=='POL' | team2=='POL') %>%\n",
    "    select(id, mdate) %>% \n",
    "    group_by(id, mdate) %>% \n",
    "    summarise(goals=n())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.\n",
    "**For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A grouped_df: 5 × 3</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>id</th><th scope=col>mdate</th><th scope=col>goals</th></tr>\n",
       "\t<tr><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>1008</td><td>9 June 2012 </td><td>1</td></tr>\n",
       "\t<tr><td>1010</td><td>13 June 2012</td><td>2</td></tr>\n",
       "\t<tr><td>1012</td><td>17 June 2012</td><td>2</td></tr>\n",
       "\t<tr><td>1026</td><td>22 June 2012</td><td>4</td></tr>\n",
       "\t<tr><td>1030</td><td>28 June 2012</td><td>1</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A grouped\\_df: 5 × 3\n",
       "\\begin{tabular}{lll}\n",
       " id & mdate & goals\\\\\n",
       " <int> & <chr> & <int>\\\\\n",
       "\\hline\n",
       "\t 1008 & 9 June 2012  & 1\\\\\n",
       "\t 1010 & 13 June 2012 & 2\\\\\n",
       "\t 1012 & 17 June 2012 & 2\\\\\n",
       "\t 1026 & 22 June 2012 & 4\\\\\n",
       "\t 1030 & 28 June 2012 & 1\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A grouped_df: 5 × 3\n",
       "\n",
       "| id &lt;int&gt; | mdate &lt;chr&gt; | goals &lt;int&gt; |\n",
       "|---|---|---|\n",
       "| 1008 | 9 June 2012  | 1 |\n",
       "| 1010 | 13 June 2012 | 2 |\n",
       "| 1012 | 17 June 2012 | 2 |\n",
       "| 1026 | 22 June 2012 | 4 |\n",
       "| 1030 | 28 June 2012 | 1 |\n",
       "\n"
      ],
      "text/plain": [
       "  id   mdate        goals\n",
       "1 1008 9 June 2012  1    \n",
       "2 1010 13 June 2012 2    \n",
       "3 1012 17 June 2012 2    \n",
       "4 1026 22 June 2012 4    \n",
       "5 1030 28 June 2012 1    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>% \n",
    "    inner_join(goal, by=c(id=\"matchid\")) %>%\n",
    "    filter(teamid=='GER') %>%\n",
    "    group_by(id, mdate) %>%\n",
    "    summarise(goals=n())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.\n",
    "List every match with the goals scored by each team as shown. This will use \"CASE WHEN\" which has not been explained in any previous exercises.\n",
    "\n",
    "  mdate | team1 | score1 | team2 | score2\n",
    "--------|-------|--------|-------|----------\n",
    "1 July 2012 | ESP | 4 | ITA | 0\n",
    "10 June 2012 | ESP | 1 | ITA | 1\n",
    "10 June 2012 | IRL | 1 | CRO | 3\n",
    "... | ... | ... | ... | ... | ...\n",
    "\n",
    "Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1. **Sort your result by mdate, matchid, team1 and team2.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<caption>A tibble: 31 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>mdate</th><th scope=col>team1</th><th scope=col>score1</th><th scope=col>team2</th><th scope=col>score2</th></tr>\n",
       "\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>1 July 2012 </td><td>ESP</td><td>4</td><td>ITA</td><td>0</td></tr>\n",
       "\t<tr><td>10 June 2012</td><td>ESP</td><td>1</td><td>ITA</td><td>1</td></tr>\n",
       "\t<tr><td>10 June 2012</td><td>IRL</td><td>1</td><td>CRO</td><td>3</td></tr>\n",
       "\t<tr><td>11 June 2012</td><td>FRA</td><td>1</td><td>ENG</td><td>1</td></tr>\n",
       "\t<tr><td>11 June 2012</td><td>UKR</td><td>2</td><td>SWE</td><td>1</td></tr>\n",
       "\t<tr><td>12 June 2012</td><td>GRE</td><td>1</td><td>CZE</td><td>2</td></tr>\n",
       "\t<tr><td>12 June 2012</td><td>POL</td><td>1</td><td>RUS</td><td>1</td></tr>\n",
       "\t<tr><td>13 June 2012</td><td>DEN</td><td>2</td><td>POR</td><td>3</td></tr>\n",
       "\t<tr><td>13 June 2012</td><td>NED</td><td>1</td><td>GER</td><td>2</td></tr>\n",
       "\t<tr><td>14 June 2012</td><td>ITA</td><td>1</td><td>CRO</td><td>1</td></tr>\n",
       "\t<tr><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>\n",
       "\t<tr><td>21 June 2012</td><td>CZE</td><td>0</td><td>POR</td><td>1</td></tr>\n",
       "\t<tr><td>22 June 2012</td><td>GER</td><td>4</td><td>GRE</td><td>2</td></tr>\n",
       "\t<tr><td>23 June 2012</td><td>ESP</td><td>2</td><td>FRA</td><td>0</td></tr>\n",
       "\t<tr><td>24 June 2012</td><td>ENG</td><td>0</td><td>ITA</td><td>0</td></tr>\n",
       "\t<tr><td>27 June 2012</td><td>POR</td><td>0</td><td>ESP</td><td>0</td></tr>\n",
       "\t<tr><td>28 June 2012</td><td>GER</td><td>1</td><td>ITA</td><td>2</td></tr>\n",
       "\t<tr><td>8 June 2012 </td><td>POL</td><td>1</td><td>GRE</td><td>1</td></tr>\n",
       "\t<tr><td>8 June 2012 </td><td>RUS</td><td>4</td><td>CZE</td><td>1</td></tr>\n",
       "\t<tr><td>9 June 2012 </td><td>NED</td><td>0</td><td>DEN</td><td>1</td></tr>\n",
       "\t<tr><td>9 June 2012 </td><td>GER</td><td>1</td><td>POR</td><td>0</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A tibble: 31 × 5\n",
       "\\begin{tabular}{lllll}\n",
       " mdate & team1 & score1 & team2 & score2\\\\\n",
       " <chr> & <chr> & <int> & <chr> & <int>\\\\\n",
       "\\hline\n",
       "\t 1 July 2012  & ESP & 4 & ITA & 0\\\\\n",
       "\t 10 June 2012 & ESP & 1 & ITA & 1\\\\\n",
       "\t 10 June 2012 & IRL & 1 & CRO & 3\\\\\n",
       "\t 11 June 2012 & FRA & 1 & ENG & 1\\\\\n",
       "\t 11 June 2012 & UKR & 2 & SWE & 1\\\\\n",
       "\t 12 June 2012 & GRE & 1 & CZE & 2\\\\\n",
       "\t 12 June 2012 & POL & 1 & RUS & 1\\\\\n",
       "\t 13 June 2012 & DEN & 2 & POR & 3\\\\\n",
       "\t 13 June 2012 & NED & 1 & GER & 2\\\\\n",
       "\t 14 June 2012 & ITA & 1 & CRO & 1\\\\\n",
       "\t ... & ... & ... & ... & ...\\\\\n",
       "\t 21 June 2012 & CZE & 0 & POR & 1\\\\\n",
       "\t 22 June 2012 & GER & 4 & GRE & 2\\\\\n",
       "\t 23 June 2012 & ESP & 2 & FRA & 0\\\\\n",
       "\t 24 June 2012 & ENG & 0 & ITA & 0\\\\\n",
       "\t 27 June 2012 & POR & 0 & ESP & 0\\\\\n",
       "\t 28 June 2012 & GER & 1 & ITA & 2\\\\\n",
       "\t 8 June 2012  & POL & 1 & GRE & 1\\\\\n",
       "\t 8 June 2012  & RUS & 4 & CZE & 1\\\\\n",
       "\t 9 June 2012  & NED & 0 & DEN & 1\\\\\n",
       "\t 9 June 2012  & GER & 1 & POR & 0\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A tibble: 31 × 5\n",
       "\n",
       "| mdate &lt;chr&gt; | team1 &lt;chr&gt; | score1 &lt;int&gt; | team2 &lt;chr&gt; | score2 &lt;int&gt; |\n",
       "|---|---|---|---|---|\n",
       "| 1 July 2012  | ESP | 4 | ITA | 0 |\n",
       "| 10 June 2012 | ESP | 1 | ITA | 1 |\n",
       "| 10 June 2012 | IRL | 1 | CRO | 3 |\n",
       "| 11 June 2012 | FRA | 1 | ENG | 1 |\n",
       "| 11 June 2012 | UKR | 2 | SWE | 1 |\n",
       "| 12 June 2012 | GRE | 1 | CZE | 2 |\n",
       "| 12 June 2012 | POL | 1 | RUS | 1 |\n",
       "| 13 June 2012 | DEN | 2 | POR | 3 |\n",
       "| 13 June 2012 | NED | 1 | GER | 2 |\n",
       "| 14 June 2012 | ITA | 1 | CRO | 1 |\n",
       "| ... | ... | ... | ... | ... |\n",
       "| 21 June 2012 | CZE | 0 | POR | 1 |\n",
       "| 22 June 2012 | GER | 4 | GRE | 2 |\n",
       "| 23 June 2012 | ESP | 2 | FRA | 0 |\n",
       "| 24 June 2012 | ENG | 0 | ITA | 0 |\n",
       "| 27 June 2012 | POR | 0 | ESP | 0 |\n",
       "| 28 June 2012 | GER | 1 | ITA | 2 |\n",
       "| 8 June 2012  | POL | 1 | GRE | 1 |\n",
       "| 8 June 2012  | RUS | 4 | CZE | 1 |\n",
       "| 9 June 2012  | NED | 0 | DEN | 1 |\n",
       "| 9 June 2012  | GER | 1 | POR | 0 |\n",
       "\n"
      ],
      "text/plain": [
       "    mdate        team1 score1 team2 score2\n",
       "1   1 July 2012  ESP   4      ITA   0     \n",
       "2   10 June 2012 ESP   1      ITA   1     \n",
       "3   10 June 2012 IRL   1      CRO   3     \n",
       "4   11 June 2012 FRA   1      ENG   1     \n",
       "5   11 June 2012 UKR   2      SWE   1     \n",
       "6   12 June 2012 GRE   1      CZE   2     \n",
       "7   12 June 2012 POL   1      RUS   1     \n",
       "8   13 June 2012 DEN   2      POR   3     \n",
       "9   13 June 2012 NED   1      GER   2     \n",
       "10  14 June 2012 ITA   1      CRO   1     \n",
       "... ...          ...   ...    ...   ...   \n",
       "22  21 June 2012 CZE   0      POR   1     \n",
       "23  22 June 2012 GER   4      GRE   2     \n",
       "24  23 June 2012 ESP   2      FRA   0     \n",
       "25  24 June 2012 ENG   0      ITA   0     \n",
       "26  27 June 2012 POR   0      ESP   0     \n",
       "27  28 June 2012 GER   1      ITA   2     \n",
       "28  8 June 2012  POL   1      GRE   1     \n",
       "29  8 June 2012  RUS   4      CZE   1     \n",
       "30  9 June 2012  NED   0      DEN   1     \n",
       "31  9 June 2012  GER   1      POR   0     "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game %>%\n",
    "    left_join(goal, by=c(id=\"matchid\")) %>% \n",
    "    mutate(goal1=if_else(is.na(teamid), FALSE, team1==teamid), \n",
    "           goal2=if_else(is.na(teamid), FALSE, team2==teamid)) %>%\n",
    "    group_by(id, mdate, team1, team2) %>% \n",
    "    summarise(score1=sum(goal1),\n",
    "              score2=sum(goal2)) %>%\n",
    "    arrange(mdate, id) %>% \n",
    "    ungroup() %>%\n",
    "    select(mdate, team1, score1, team2, score2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "dbDisconnect(con)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
